package com.market.service.raffle;

import com.market.constants.Constants;
import com.market.domain.entity.RaffleAwardEntity;
import com.market.domain.entity.RaffleFactorEntity;
import com.market.domain.entity.RuleActionEntity;
import com.market.domain.entity.StrategyEntity;
import com.market.domain.po.Strategy;
import com.market.domain.vo.RuleLogicCheckTypeVO;
import com.market.enums.ResponseCode;
import com.market.exception.AppException;
import com.market.factory.DefaultLogicFactory;
import com.market.service.RaffleStrategy;
import com.market.service.StrategyArmoryService;
import com.market.service.StrategyDispatch;
import io.micrometer.core.instrument.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

/**
 * 抽奖策略抽象类，定义抽奖流程
 */
@Slf4j
public abstract class AbstractRaffleStrategy implements RaffleStrategy {

    protected StrategyDispatch strategyDispatch;
    protected StrategyArmoryService strategyArmoryService;

    public AbstractRaffleStrategy(StrategyDispatch strategyDispatch){
        this.strategyDispatch=strategyDispatch;
    }

    @Override
    public RaffleAwardEntity performRaffle(RaffleFactorEntity raffleFactorEntity) {

        //参数校验
        String userId = raffleFactorEntity.getUserId();
        Long strategyId = raffleFactorEntity.getStrategyId();
        if (null == strategyId || StringUtils.isBlank(userId)) {
            throw new AppException(ResponseCode.ILLEGAL_PARAMETER.getCode(), ResponseCode.ILLEGAL_PARAMETER.getInfo());
        }

        // 2. 策略查询
        StrategyEntity strategy = strategyArmoryService.queryStrategyEntityByStrategyId(strategyId);

        // 3. 抽奖前 - 规则过滤
        RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> ruleActionEntity = this.doCheckRaffleBeforeLogic(RaffleFactorEntity.builder().userId(userId).strategyId(strategyId).build(), strategy.ruleModels());

        if(ruleActionEntity.getCode().equals(RuleLogicCheckTypeVO.TAKE_OVER.getCode())){
            if(ruleActionEntity.getRuleModel().equals(DefaultLogicFactory.LogicModel.RULE_BLACKLIST.getCode())){
                //黑名单用户
                return RaffleAwardEntity.builder()
                        .awardId(ruleActionEntity.getData().getAwardId())
                        .build();
            }else if(ruleActionEntity.getRuleModel().equals(DefaultLogicFactory.LogicModel.RULE_WIGHT.getCode())){
                //权重,根据返回信息抽奖
                RuleActionEntity.RaffleBeforeEntity raffleBeforeEntity=ruleActionEntity.getData();//拿子类
                String ruleWeightValueKey = raffleBeforeEntity.getRuleWeightValueKey();
                //开抽
                Integer randomAwardId = strategyDispatch.getRandomAwardId(strategyId, ruleWeightValueKey);
                return RaffleAwardEntity.builder().awardId(randomAwardId).build();

            }
        }

        // 4默认抽奖流程
        Integer awardId = strategyDispatch.getRandomAwardId(strategyId);

        return RaffleAwardEntity.builder()
                .awardId(awardId)
                .build();

    }

    protected abstract RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> doCheckRaffleBeforeLogic(RaffleFactorEntity build, String[] strings);


}
